package com.yhf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhf.dto.SubitemDto;
import com.yhf.mapper.SubitemMapper;
import com.yhf.mapper.PMapper;
import com.yhf.mapper.SubitemMapper;
import com.yhf.model.Subitem;
import com.yhf.model.Project;
import com.yhf.model.Subitem;
import com.yhf.service.SubitemService;
import com.yhf.utils.PageResult;
import com.yhf.utils.QueryPageBean;
import com.yhf.utils.Result;
import com.yhf.vo.SubitemProjectOv;
import com.yhf.vo.ProjectEmag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SubitemServiceImpl extends ServiceImpl<SubitemMapper, Subitem> implements SubitemService {

    @Autowired
    SubitemMapper subitemMapper;
    @Autowired
    PMapper pMapper;
    @Override
    public PageResult pageList(QueryPageBean queryPageBean){
        Page<SubitemDto> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<SubitemDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        IPage<SubitemDto> page1 = subitemMapper.selectAll(page, queryWrapper);
        PageResult pageResult = new PageResult();
        pageResult.setRows(page1.getRecords());
        pageResult.setTotal(page1.getTotal());
        return pageResult;
    }

    @Override
    public Result add(Subitem subitem) {
        int insert = subitemMapper.insert(subitem);
        if (insert >0) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result findStageById(String suid) {
        Subitem subitem = subitemMapper.selectById(suid);
        Result result = new Result();
        result.setFlag(true);
        result.setMessage("success");
        result.setData(subitem);
        return result;
    }

    @Override
    public Result editByStage(Subitem subitem) {
        subitem.setUpdatetime(new Date());
        int i = subitemMapper.updateById(subitem);
        if (i >0) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result deleteBySid(String suid) {
        Subitem subitem = new Subitem();
        subitem.setStatus(0);
        subitem.setSuid(suid);
        int delete = subitemMapper.updateById(subitem);
        if (delete >0) {
            return Result.success();
        }
        return Result.fail();
    }


    @Override
    public Result findSp(String suid) {
        System.out.println(suid);
        Result result = new Result();
        SubitemProjectOv subitemProjectOv = new SubitemProjectOv();
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<Project> projects = pMapper.selectList(queryWrapper);
        ProjectEmag[] projectEmags = new ProjectEmag[projects.size()];
        for (int i = 0;i<projects.size();i++) {
            ProjectEmag projectEmag = new ProjectEmag();
            projectEmag.setValue(projects.get(i).getPid());
            projectEmag.setLabel(projects.get(i).getPname());
            projectEmags[i] = projectEmag;
        }
        subitemProjectOv.setProjectEmags(projectEmags);
        result.setData(subitemProjectOv);
        if (suid != null) {
            Subitem subitem = subitemMapper.selectById(suid);
            subitemProjectOv.setChecked(subitem.getPid());
        }
        result.setFlag(true);
        result.setMessage("success");
        return result;
    }

}
